/**
 * 通用登录窗口
 */
Rsd.define('Rsd.tool.LoginDialog', {
    extend: 'Rsd.container.Dialog',
    requires: [
        'Rsd.form.Button',
        'Rsd.form.Label',
        'Rsd.form.Link',
        'Rsd.form.CheckBox',
        'Rsd.form.Image',
        'Rsd.control.IframeBox'
    ],
    width:800,
    height:650,
    title:'用户登录',
    closeBtn:false, 
    layout: {
        type:'vbox',
        align:'center',
        content:'start end'
    },
    items:[
        //0：用户名（账号，手机号，邮箱）+ 密码 登录
        {
            xtype: 'container',
            //margin: '70 0 0 5%',
            border: false,
            height: 350,
            width:'80%',
            style:{maxWidth:600},
            layout: {type:'vbox',align:'start',content:'start end'},
            items: [
                {
                    xtype: 'input',
                    inputType: 'text',
                    label: {content: '用户名', width: 80,height:35,style:{fontSize:'120%'}},
                    height: 40,
                    width:'60%', 
                    emptyText: 'user id',
                    value: '',
                    margin: '0 0 0 20'
                },
                {
                    xtype: 'input',
                    inputType: 'password',
                    label: {content: '密&nbsp;&nbsp;&nbsp;码', width: 80,height:35,style:{fontSize:'120%'}},
                    height: 40,
                    width:'60%',
                    emptyText: 'password',
                    margin: '25 0 0 20'
                },
                {
                    xtype: 'checkbox',
                    label: {content: '记住密码', position: 'right', align: 'left',width:'auto'},
                    height: 30,
                    width:80,
                    margin: '20 0 15 130'
                },
                {
                    xtype: 'button',
                    text: '登 录', 
                    height:50,
                    width:'45%',
                    margin: '10 30 5 110',
                    accessKey:'enter',
                    border: false,
                    style:{
                        element:'ctrl',
                        borderColor:"#2182e6bf",
                        backgroundColor:"#2182e6bf",
                        color:"#ebebf5", 
                        fontSize:"15px"
                    },
                    handler:'btn_login'
                }
            ]
        },
        //1:扫码登录
        {
             xtype:'iframe', 
             width:'50%',
             label:{height:50,position:'top'},
             height: 350,
             hidden:true,
             method:"get",
             style:{color:'#000'},
             ctrl:{ style:{overflow:'hidden'},},
             //margin: '0 0 0 25%',
             usingMessage:true,
             handleMessage:'handleSessionMessage',
             src:''
        },
        //2:手机号 + 验证码 登录
        {
            xtype:'container',
            //margin: '70 0 0 10%',
            border: false,
            height: 350,
            hidden:true,
            width:'50%',
            layout: 'vbox', 
            header:false,
            items:[
                {
                    xtype:'label',
                    label:'未开通手机号登录服务',
                    height:50,
                    style:{color:'red'}
                }

            ]
        },
        //3:邮箱 + 验证码 登录
        {
            xtype:'container',
            //margin: '70 0 0 10%',
            border: false,
            height: 350,
            hidden:true,
            width:'50%',
            layout: 'vbox',
            header:false,
            items:[
                {
                    xtype:'label',
                    label:'未开通邮箱登录服务',
                    height:50,
                    style:{color:'red'}
                } 
            ]
        },
        //4:loading
        {
            xtype: 'image',
            id: 'loading',
            margin: '10 50 40 90',
            label: { content: '正在验证信息,请稍等...', position: 'right', lineHeight: 40, width: 400 },
            toolbar: false,
            src: '',
            visible: false,
            border: false,
            width: 430,
            height: 40
        },
        //5:
        {
            xtype: 'container',
            header:{
                content:'其他登录方式',
                height:50,
                style:{backgroundColor:'transparent',borderBottom:'1px #d5d2d0 solid'}
            },
            height:150,
            width:'90%',
            style:{
                maxWidth:800
            },
            margin: '0 1% 0 1%',
            layout:'hbox',
            items:[ 
                {
                    xtype:'image',
                    height:70, 
                    flex:1,
                    hidden:true,
                    imageStyle:{width:50,height:50,borderRadius:'25px'},
                    label:{content:'用户名密码登录',position:'bottom',aling:'center'},
                    toolbar:false,
                    src:'',
                    handler:'btn_password'
                },
                {
                    xtype:'image',
                    height:70, 
                    flex:1,
                    imageStyle:{width:50,height:50,borderRadius:'25px'},
                    label:{content:'微信扫码登录',position:'bottom',aling:'center'},
                    toolbar:false,
                    src:'',
                    handler:'btn_wechat'
                }, 
                {
                    xtype:'image',
                    height:70, 
                    flex:1,
                    imageStyle:{width:50,height:50,borderRadius:'25px'},
                    label:{content:'手机号登录',position:'bottom',aling:'center'},
                    toolbar:false,
                    src:'',
                    handler:'btn_mobile'
                }, 
                {
                    xtype:'image',
                    height:70, 
                    flex:1,
                    imageStyle:{width:50,height:50,borderRadius:'25px'},
                    label:{content:'邮箱登录',position:'bottom',aling:'center'},
                    toolbar:false,
                    src:'',
                    handler:'btn_email'
                } 

            ]
            
        }
    ],  
    wxConfigKey:'49e75756-12ec-4dc5-9def-a6a4c4757dc4',
    color:'black',
    services:{
        checkSession:'',//Session 验证接口
        loginByPassword:'',//密码登录接口
        loginByCode:''//验证码登录接口
    },
     /**
     * 
     * @param {*} config 
     */
     constructor: function LoginDialog(config) {
        config = config || {};
        this.apply(config);
        
         //微信下登录方式
         if(Rsd.isWechat && Rsd.isWechat())
         {  
         }
 
        // this.items[0].label.position = 'right';
        // this.items[0].label.height = '100%';
        // this.items[0].label.width = 100;
    },
     
    /**
     * 
     */
    load:function load()
    {
        this.callParent();

        this.items[0].items[0].value = Rsd.getCookie("userid");
        this.items[0].items[1].value = Rsd.getCookie("pwd");
        this.items[0].items[2].checked = Rsd.getCookie("pwd").length > 0;
        this.registerHotKey('Enter',this.login);
        this.registerHotKey('ctrl+Enter',this.login);

        //
        this.items[4].setSrc(Rsd.getRedjsUrl('/resources/images/control/login/loading.gif'));
        this.items[5].items[0].setSrc(Rsd.getRedjsUrl('/resources/images/control/login/password.png'));
        this.items[5].items[1].setSrc(Rsd.getRedjsUrl('/resources/images/control/login/qrcode.png'));
        this.items[5].items[2].setSrc(Rsd.getRedjsUrl('/resources/images/control/login/mobile.png'));
        this.items[5].items[3].setSrc(Rsd.getRedjsUrl('/resources/images/control/login/email.png'));
    },
    /**
     * 
     */
    btn_login:function btn_login()
    { 
        var me = this;
        me.items[4].visible = true;

        var _userId = me.items[0].items[0].getValue();
        var _pwd = me.items[0].items[1].getValue();

        this.login(_userId,_pwd,function(){

            me.items[4].visible=false;
        });
          
    },
    /**
     * 
     */
    btn_password:function btn_password()
    {
        this.items[0].show();
        this.items[1].hide();
        this.items[2].hide();
        this.items[3].hide();
        this.items[4].setVisible(false);
        this.items[5].items[0].hide();
        this.items[5].items[1].show();
        this.items[5].items[2].show();
        this.items[5].items[3].show();
    },
    /**
     * 
     */
    btn_wechat:function btn_wechat()
    {
        var scope = "snsapi_login";
        var returnType="html";
        //微信浏览器下登录方式
        if(Rsd.isWechat&&Rsd.isWechat())
        { 
            returnType="redirect"  
            scope = "snsapi_userinfo";
        } 
        this.items[0].hide();
        this.items[1].setSrc("http://vx.sellerpartner.cn/wxop/oauth2/login/"+this.wxConfigKey+"?state=LoginSP&style="+(this.color||'black')+"&self=true&css=%2F%2Fvx.sellerpartner.cn%2Fwxop%2Foauth2%2Fcss%2F"+this.wxConfigKey+"&returnType="+returnType+"&scope="+scope);
        this.items[1].show();
        this.items[2].hide();
        this.items[3].hide();
        this.items[4].setVisible(false);
        this.items[5].items[0].show();
        this.items[5].items[1].hide();
        this.items[5].items[2].show();
        this.items[5].items[3].show();
    },
    /**
     * 
     */
    btn_mobile:function btn_mobile()
    {
        alert('未开通手机号登录服务');

        this.items[0].hide();
        this.items[1].hide();
        this.items[2].show();
        this.items[3].hide();
        this.items[4].setVisible(false);
        this.items[5].items[0].show();
        this.items[5].items[1].show();
        this.items[5].items[2].hide();
        this.items[5].items[3].show();
    },
    /**
     * 
     */
    btn_email:function btn_email()
    {
        alert('未开通邮箱登录服务');

        this.items[0].hide();
        this.items[1].hide();
        this.items[2].hide();
        this.items[3].show();
        this.items[4].setVisible(false);
        this.items[5].items[0].show();
        this.items[5].items[1].show();
        this.items[5].items[2].show();
        this.items[5].items[3].hide();
    },
    /*
    * */
    login:function login(_userId,_pwd,callback)
    {
      
        var me = this;
       
        if(_userId.length == 0 || _pwd.length == 0)
        {
            Rsd.alert("用户名和密码不允许为空！");
            return;
        }
        //  debugger;
        Rsd.requestService('mission.account.login', {
            userid: _userId,
            pwd:_pwd//Rsd.md5(_pwd)
        },function(_data){
            if (_data == null) {
                if(callback)
                {
                    callback();
                }
                Rsd.alert('请求验证失败，请联系管理员！');
                return;
            }

            if (_data.success) {

                Rsd.addCookie("userid",_userId);
                //debugger;
                if(me.items[0].items[2].isChecked())
                {
                    Rsd.addCookie("pwd",_pwd);
                }
                else
                {
                    Rsd.deleteCookie("pwd");
                }

                Rsd.addCookie("token", _data.data.token);
                Rsd.setAppToken(_data.data.token);
                Rsd.app.user = _data.data;
                Rsd.app.userName =  _data.data.XingMing||'';

                me.close();
            } else {
                //debugger; 
                Rsd.alert(_data.msg||'登录失败！');
                 if(callback)
                {
                    callback();
                }
                return false;
            }
        });


    },
    /**
     * 接受来自扫码登录 iframe的信息
     * @param {*} data 
     * @param {*} origin 
     */
    handleSessionMessage:function handleSessionMessage(data,origin)
    {
        //console.log('**** handle Session Message ******',arguments); 

        if(origin !== 'http://vx.sellerpartner.cn')
        {
            return;
        }
        var me = this;
        var _token = data.token;
        if(_token)
        { 
            Rsd.setAppToken(_token);//必须
            Rsd.requestService(me.services.checkSession, {token: _token}, function (resp) {
                //console.log('handleMessage checkSession respone',resp); 
            }); 

        }

        me.close();
        
    },
    /*
    * */
    cancel:function cancel()
    {
        Rsd.showMessage("您确定关闭该窗口？",[
                {
                    btn:'确 定',
                    fn:function(){
                        Rsd.closeWindow();
                    }
                },
                {
                    btn:'取 消',
                    fn:function(){ }
                }
            ]
        );

    }
});